package org.lego.biz.task.management.base.service;

import org.lego.biz.task.management.base.entity.Task;
import org.lego.biz.task.management.base.repository.TaskRepository;
import org.lego.jpa.base.repository.BaseRepository;
import org.lego.jpa.base.service.BaseService;
import org.lego.jpa.base.util.DynamicSpecifications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hr.qjy on 16/11/18.
 */
@Service
public class TaskService extends BaseService<Task, String> {
    public static final int PAGE_SIZE = 10;
    @Autowired
    private TaskRepository taskRepository;

    @Override
    protected BaseRepository<Task, String> getRepository() {
        return taskRepository;
    }

    /**
     * 查询任务第一页 返回10条数据
     *
     * @param moduleId 任务模块id
     * @return
     */
    public List<Task> getFirstPage(String moduleId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("EQ_status", Task.STATUS_NORMAL);
        params.put("EQ_taskModule.id", moduleId);
        Specification<Task> specification = DynamicSpecifications.buildSpecification(params, Task.class);
        PageRequest pageRequest = new PageRequest(0, PAGE_SIZE);
        return this.findAll(specification, pageRequest).getContent();
    }

    /**
     * 查询总数
     *
     * @param moduleId 任务模块id
     * @return
     */
    public Long getTotal(String moduleId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("EQ_status", Task.STATUS_NORMAL);
        params.put("EQ_taskModule.id", moduleId);
        Specification<Task> specification = DynamicSpecifications.buildSpecification(params, Task.class);
        return this.count(specification);
    }


}
